package com.tianma315.admin.product.rest;

import com.baomidou.mybatisplus.plugins.Page;
import com.tianma315.admin.common.base.BaseController;
import com.tianma315.admin.common.utils.Result;
import com.tianma315.admin.product.domain.Product;
import com.tianma315.admin.product.domain.vo.ProductVo;
import com.tianma315.admin.product.service.ProductService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductRestController extends BaseController {

    @Autowired
    private ProductService productService;

    /**
     * 产品列表
     *
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @GetMapping("/list")
    @RequiresPermissions("product:list")
    Result<Page<ProductVo>> page(int pageNumber, int pageSize, String searchKey) {
        Page<ProductVo> page = productService.getPage(pageNumber, pageSize,searchKey);
        if (page != null)
            return Result.ok(page);
        return Result.fail();
    }


    /**
     * 添加产品
     *
     * @param product
     * @return
     */
    @PostMapping("/add")
    @RequiresPermissions("product:add")
    Result add(@Validated Product product) {
        if (productService.save(product))
            return Result.ok();
        return Result.fail();
    }

    /**
     * 添加产品
     *
     * @param productId
     * @return
     */
    @PostMapping("/del")
    @RequiresPermissions("product:delete")
    Result del(long productId) {
        if (productService.remove(productId))
            return Result.ok();
        return Result.fail();
    }

    /**
     * 添加产品
     *
     * @param product
     * @return
     */
    @PostMapping("/edit")
    @RequiresPermissions("product:edit")
    Result edit(@Validated Product product) {
        if (productService.edit(product))
            return Result.ok();
        return Result.fail();
    }

    /**
     * 获取列表
     */
    @GetMapping("/getProductList")
    @RequiresPermissions("product:edit")
    Result<List<Product>> getProductList(Product product) {
        List<Product> list = productService.getAll(product);
        return Result.ok(list);
    }
    /**
     * 获取列表
     */
    @GetMapping("/getProductListByName")
    @RequiresPermissions("product:edit")
    Result<List<Product>> lists(String productName) {
        List<Product> list = productService.getAllByName(productName);
        return Result.ok(list);
    }

}
